package com.comtime.view;


import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.comtime.smartkilimanjaro.R;


public class SelectDevicePopupWindow extends PopupWindow {

	View view;
	
	private OnDeviceSelectListener selectListener;
	
	private  String[] displayValues;
	
	private Context context;
	
	public SelectDevicePopupWindow(Context context,OnDeviceSelectListener listener, String[] values) {
		super(context);
		this.context=context;
		selectListener=listener;
		displayValues=values;
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		view = inflater.inflate(R.
						layout.popupwindow_select_device,
				null);
		
		
		ListView listView=(ListView) view.findViewById(R.id.listview);
		
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				 
				if(selectListener!=null){
					SelectDevicePopupWindow.this.dismiss();
					selectListener.onSelected(position, displayValues[position]);
				}
			}
		});
		 
		// ����SelectPicPopupWindow��View
		this.setContentView(view);
		// ����SelectPicPopupWindow��������Ŀ�
		this.setWidth(LayoutParams.FILL_PARENT);
		// ����SelectPicPopupWindow��������ĸ�
		this.setHeight(LayoutParams.FILL_PARENT);
		// ����SelectPicPopupWindow��������ɵ��
		this.setFocusable(true);
		// ����SelectPicPopupWindow�������嶯��Ч��
//		this.setAnimationStyle(R.style.AnimBottom);
		// ʵ��һ��ColorDrawable��ɫΪ͸��
		ColorDrawable dw = new ColorDrawable(0x00000000);
		// ����SelectPicPopupWindow��������ı���
		this.setBackgroundDrawable(dw);


	}
 
	
	private BaseAdapter adapter=new BaseAdapter() {
		
		@Override
		public View getView(int position, View view, ViewGroup group) {
			ViewHolder holder = null;
			if (view == null) {
				holder = new ViewHolder();
				view = LayoutInflater.from(context)
						.inflate(R.layout.item_device_mac, null);
				holder.tv_mac = (TextView) view.findViewById(R.id.tv_mac);

				view.setTag(holder);
			} else {
				holder = (ViewHolder) view.getTag();
			}
			holder.tv_mac.setText(displayValues[position]);
			
			
			return view;
		}
		
		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}
		
		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return null;
		}
		
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return displayValues==null? 0:displayValues.length;
		}
	};
	private class ViewHolder {
		TextView tv_mac;
	}
	
	public interface OnDeviceSelectListener{
		public void onSelected(int index,String mac);
	}
}
